package com.gxa.community.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.community.admin.mapper.OperateKillSpellGoodsMapper;
import com.gxa.community.admin.mapper.OperateKillSpellMapper;
import com.gxa.community.admin.service.GroupService;
import com.gxa.community.admin.service.OperateKillSpellGoodsService;
import com.gxa.community.admin.service.OperateKillSpellService;
import com.gxa.community.core.pojo.bo.SecAddGoodBo;
import com.gxa.community.core.pojo.domain.OperateKillSpell;
import com.gxa.community.core.pojo.domain.OperateKillSpellGoods;
import com.gxa.community.core.pojo.vo.SeckillAddVo;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private OperateKillSpellGoodsMapper secKillGoodsMapper;
    @Autowired
    private OperateKillSpellMapper secKillMapper;
    @Autowired
    private OperateKillSpellService operateKillSpellService;
    @Autowired
    private OperateKillSpellGoodsService secKillGoodsService;

    @Override
    @Transactional
    public R AddGroup(SeckillAddVo seckillAddVo, Integer type) {
        System.err.println(seckillAddVo);
        /*添加秒杀先去数据库里 判断进件的秒杀开始时间是否大于上一个秒杀的结束时间 是的话
        则可以新建，不是的话则返回信息 同一时间段内不能有多个秒杀活动
        */
        Long timeStart = seckillAddVo.getTimeStart();//新建的秒杀开始时间
        //获取数据库里的所有该活动
        QueryWrapper<OperateKillSpell> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        List<OperateKillSpell> operateKillSpells = secKillMapper.selectList(wrapper);
        System.err.println(operateKillSpells);
        for (OperateKillSpell operateKillSpell : operateKillSpells) {
            if (timeStart<operateKillSpell.getEndTime())
                return R.error("新建活动的开始时间不能小于已有活动的结束时间（一个时间段内只能有一个活动）");
        }
        OperateKillSpell secKill = new OperateKillSpell();
        OperateKillSpellGoods secKillGoods = new OperateKillSpellGoods();
        secKill.setName(seckillAddVo.getName());
        secKill.setStartTime(seckillAddVo.getTimeStart());
        secKill.setEndTime(seckillAddVo.getTimeEnd());
        secKill.setStatus(seckillAddVo.getStatus());
        secKill.setTime(seckillAddVo.getTime());
        secKill.setKillId(seckillAddVo.getKillId());
        secKill.setType(type);
        boolean save = operateKillSpellService.save(secKill);
        List<SecAddGoodBo> secAddGoodBo = seckillAddVo.getSecAddGoodBo();
        for (SecAddGoodBo addGoodBo : secAddGoodBo) {
            secKillGoods.setGoodsName(addGoodBo.getGoodsName());
            secKillGoods.setGoodsNo(addGoodBo.getGoodsNo());
            secKillGoods.setKillId(seckillAddVo.getKillId());
            secKillGoods.setLastnum(addGoodBo.getLastnum());
            secKillGoods.setPrice(addGoodBo.getSeckillPrice());
            secKillGoods.setNum(addGoodBo.getNum());
            secKillGoods.setSort(addGoodBo.getSort());
            secKillGoodsService.save(secKillGoods);
        }
        if (save){
            return R.ok("创建成功");
        }
        return R.error("创建失败");
    }



}
